package com.listeningframework.boot.domains;

import java.util.ArrayList;
import java.util.List;

/**
 * Listening Response Body
 *
 * @author Listening
 * @version $Id: ExtJsResponseBody.java, v 0.1 2014年12月3日 下午4:03:56 Listening
 *          Exp $
 */
public class ResponseBodys<T> {
    /**
     * 成功标示
     */
    private boolean success = true;
    /**
     * 逻辑成功标志
     */
    private String status = "0";
    /**
     * 记录数
     */
    private Integer results = 0;
    /**
     * 记录列表
     */
    private List<T> rows = new ArrayList<T>();
    /**
     * 操作提示
     */
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public String getStatus() {
        return status;
    }

    public Integer getResults() {
        return results;
    }

    public void setResults(Integer results) {
        this.results = results;
    }

    public void setResponseSuccess() {
        this.status = "1";
    }

    public void setResponseFail() {
        this.status = "0";
    }

    public void addData(T t) {
        if (t != null) {
            rows.add(t);
        }
    }

    public void addAllData(List<T> list) {
        if (list != null) {
            rows.addAll(list);
        }
    }

    /**
     * 返回失败
     *
     * @param message 失败原因
     * @param <T>     泛型类型
     * @return ResponseBodys
     */
    public static <T> ResponseBodys<T> fail(String message) {
        ResponseBodys<T> result = new ResponseBodys<>();
        result.setMessage(message);
        result.setResponseFail();
        return result;
    }

    /**
     * ExtJsResponseBody success
     *
     * @param message 信息描述
     * @param <T> 泛型类
     * @return ResponseBodys
     */
    public static <T> ResponseBodys<T> success(String message) {
        ResponseBodys<T> result = new ResponseBodys<>();
        result.setResponseSuccess();
        return result;
    }

    /**
     * Response Body Success
     *
     * @param dataList 数据列表
     * @param totalCount 数据计数
     * @param <T> 泛型类
     * @return ResponseBodys
     */
    public static <T> ResponseBodys<T> success(List<T> dataList, Integer totalCount) {
        ResponseBodys<T> result = new ResponseBodys<>();
        result.setResponseSuccess();
        result.setResults(totalCount);
        result.setRows(dataList);
        return result;
    }

}
